package cn.vastbase.jdbc.base;

import java.sql.*;

public class MetaDataTestCase {

    public static void main(String[] args) {
        String url = "jdbc:postgresql://192.168.56.12:6000/test?loggerLevel=OFF"; // 替换为您的数据库URL
        String user = "test"; // 替换为您的数据库用户名
        String password = "Vastdata#0730"; // 替换为您的数据库密码

        try (Connection conn = DriverManager.getConnection(url, user, password)) {
            DatabaseMetaData metaData = conn.getMetaData();
            String catalog = null; // 通常为null或具体的catalog名称
            String schemaPattern = null; // 可以指定schema，如"public"
            String tableNamePattern = "tb5"; // 替换为您想查询的表名
            boolean unique = false; // 是否只返回唯一索引
            boolean approximate = false; // 是否允许近似结果

            try (ResultSet rs = metaData.getIndexInfo(catalog, schemaPattern, tableNamePattern, unique, approximate)) {
                while (rs.next()) {
//                    System.out.println("Table Name: " + rs.getString("TABLE_NAME"));
                    System.out.println("Index Name: " + rs.getString("INDEX_NAME"));
//                    System.out.println("Non-unique: " + rs.getBoolean("NON_UNIQUE"));
                    System.out.println("Type: " + rs.getShort("TYPE")); // 0 = table index (统计), 1 = 普通索引, 2 = 唯一索引, 3 = 主键
                    System.out.println("Column Name: " + rs.getString("COLUMN_NAME"));
//                    System.out.println("Asc or Desc: " + rs.getString("ASC_OR_DESC")); // 升序或降序
//                    System.out.println("Ordinal Position: " + rs.getShort("ORDINAL_POSITION"));
//                    System.out.println("Cardinality: " + rs.getInt("CARDINALITY")); // 索引中的不同值的数量
//                    System.out.println("Pages: " + rs.getInt("PAGES")); // 包含索引的页数
//                    System.out.println("Filter Condition: " + rs.getString("FILTER_CONDITION")); // 过滤条件
                    System.out.println("-----------------------------");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
